import { createRouter, createWebHistory } from 'vue-router'

// 动态获取配置
const pages = import.meta.glob('../views/**/page.js', {
  eager: true,
  import: 'default',
})

const components = import.meta.glob('../views/**/*.vue')

// 自动生成路由配置
const routes = Object.entries(pages).map(([path,meta])=>{
  console.log(path,meta)
  let vueName = "index.vue"
  if (meta.vueName !== ""){
    vueName =  meta.vueName + ".vue"
  }

  const pageJsPath = path
  const componentPath = pageJsPath.replace('page.js', vueName)

  path = path.replace('../views','').replace("/page.js","") || "/"
  const name = path.split('/').filter(Boolean).join('-') || "index"

  return {
    path,
    name,
    component: components[componentPath],
    //component: () => import(componentPath),
    meta
  }
})

// 获得完整路由
function getAllRouter() {
  const extraRouter = [{
    path: '/',
    redirect: '/home'
  }]
  return [...routes, ...extraRouter]
}
console.log(routes)

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: getAllRouter(),
/*  routes: [
    {
      path: '/',
      name: 'home',
      component: () => import('../views/home/index.vue')
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/about/index.vue')
    },
    {
      path: '/task',
      name: 'task',
      component: () => import('../views/task/Task.vue')
    }
  ]*/
})

export default router
